package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import dao.DBUtil;
import dao.DepartmentOperation;
import entity.Departerment;

public class Student_Info_Panel extends JPanel implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField[] tf = new JTextField[3];
	private JButton[] but = new JButton[4];
	private DefaultTableModel dtm;
	private JButton[] crossBut = new JButton[2];
	private JLabel infolab;
    private JButton[] butPage=new JButton[4];
    private DepartmentOperation dto;
    private JLabel lab4;
    private int maxPageNumber,currentPageNumber=1;
    private double pageSize=15;
    private JTable table;
    private List<Departerment> stu;
	
	public Student_Info_Panel() {
		// TODO Auto-generated constructor stub
		GridBagLayout layout = new GridBagLayout();
		setLayout(layout);

		Font font = new Font("宋体", Font.BOLD, 20);
		Font font1 = new Font("宋体", Font.BOLD, 15);
		Font font2 = new Font("宋体", Font.BOLD, 13);

		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;

		for (int i = 0; i < 10; i++) {
			gbc.gridx = 0;
			gbc.gridy = i;
			gbc.weighty = 0;
			gbc.weightx = 0;
			JLabel lab = new JLabel();
			gbc.insets = new Insets(5, 0, 5, 0);
			layout.setConstraints(lab, gbc);
			add(lab);
		}

		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridheight = 8;
		gbc.weighty = 0;
		gbc.weightx = 0.3;
		dtm = new DefaultTableModel(new Object[][] {}, new String[] { "ID", "名称", "负责人" });
		table = new JTable(dtm);
		table.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 13));
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table.setRowHeight(43);
		table.setBorder(BorderFactory.createRaisedBevelBorder());
		JTableHeader head=table.getTableHeader();
		head.setFont(new Font("微软雅黑", Font.BOLD, 15));
		head.setPreferredSize(new Dimension(head.getWidth(), 35));
		JScrollPane sp = new JScrollPane(table);
		gbc.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(sp, gbc);
		add(sp);

		String[] pageName={"首页","上一页","下一页","尾页"};
		for(int i=0;i<pageName.length;i++){
			gbc.gridx=1;
			gbc.gridy=8;
			gbc.weightx=0;
			gbc.weighty=0;
			lab4=new JLabel();
			lab4.setLayout(new FlowLayout());
			gbc.insets=new Insets(5, 5, 5, 5);
			layout.setConstraints(lab4, gbc);
			add(lab4);
		}
		
		for (int i = 0; i < pageName.length; i++) {
			butPage[i]=new JButton(pageName[i]);
			butPage[i].setFont(font2);
			butPage[i].setSize(80, 30);
			butPage[i].addActionListener(this);
			butPage[i].setEnabled(false);
			lab4.add(butPage[i]);
		}
		
		for (int i = 0; i < 4; i++) {
			gbc.gridx = 2 + i;
			gbc.gridy = 0;
			gbc.gridwidth = 1;
			gbc.gridheight = 1;
			gbc.weightx = 0;
			gbc.weighty = 0;
			JLabel lab1 = new JLabel();
			gbc.insets = new Insets(5, 5, 5, 5);
			layout.setConstraints(lab1, gbc);
			add(lab1);
		}

		gbc.gridx = 2;
		gbc.gridy = 1;
		gbc.gridwidth = 0;
		gbc.gridheight = 1;
		gbc.weighty = 1;
		gbc.weightx = 0;
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		infolab = new JLabel(DBUtil.getMessage());
		infolab.setFont(font);
		infolab.setHorizontalAlignment(JLabel.CENTER);
		panel.add(infolab, BorderLayout.CENTER);
		gbc.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(panel, gbc);
		add(panel);

		String[] labText = { "学号", "姓名", "性别" };
		for (int i = 0; i < labText.length; i++) {
			gbc.gridx = 2;
			gbc.gridy = 2 + i;
			gbc.gridheight = 1;
			gbc.weighty = 0;
			gbc.weightx = 0;
			JLabel labName1 = new JLabel(labText[i]);
			labName1.setFont(font1);
			gbc.insets = new Insets(20, 5, 0, 20);
			layout.setConstraints(labName1, gbc);
			add(labName1);

			gbc.gridx = 3;
			gbc.gridy = 2 + i;
			gbc.gridwidth = 0;
			gbc.weighty = 0;
			gbc.weightx = 0;
			tf[i] = new JTextField(20);
			gbc.insets = new Insets(20, 0, 5, 20);
			layout.setConstraints(tf[i], gbc);
			add(tf[i]);
		}

		gbc.gridx = 2;
		gbc.gridy = 5;
		gbc.gridwidth = 0;
		gbc.gridheight = 1;
		gbc.weighty = 0;
		gbc.weightx = 0;
		JPanel panel1 = new JPanel();
		gbc.insets = new Insets(20, 5, 5, 20);
		layout.setConstraints(panel1, gbc);
		add(panel1);

		String[] crossButName = { "上一条", "下一条" };
		String[] butName = { "查询", "添加", "修改", "删除" };
		for (int i = 0; i < crossBut.length; i++) {
			gbc.gridx = 3 + i;
			gbc.gridy = 6;
			gbc.gridwidth = 1;
			gbc.gridheight = 1;
			gbc.weighty = 0;
			gbc.weightx = 0;
			crossBut[i] = new JButton(crossButName[i]);
			crossBut[i].setFont(font2);
			crossBut[i].setEnabled(false);
			crossBut[i].addActionListener(this);
			gbc.insets = new Insets(20, 5, 5, 20);
			layout.setConstraints(crossBut[i], gbc);
			add(crossBut[i]);
		}

		for (int i = 0; i < butName.length; i++) {
			gbc.gridx = 2 + i;
			gbc.gridy = 7;
			gbc.gridwidth = 1;
			gbc.gridheight = 1;
			gbc.weightx = 0;
			gbc.weighty = 0;
			but[i] = new JButton(butName[i]);
			but[i].setFont(font2);
			gbc.insets = new Insets(20, 5, 5, 20);
			layout.setConstraints(but[i], gbc);
			but[i].addActionListener(this);
			add(but[i]);
		}

		gbc.gridx = 2;
		gbc.gridy = 8;
		gbc.gridwidth = 0;
		gbc.gridheight = 1;
		gbc.weighty = 1;
		gbc.weightx = 0;
		JPanel panel2 = new JPanel();
		gbc.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(panel2, gbc);
		add(panel2);

		//so = new StudentOperate();
		dto=new DepartmentOperation();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==but[0]){
			while (dtm.getRowCount() != 0) {
				dtm.removeRow(0);
			}
			if(tf[0].getText().equals("")&&tf[1].getText().equals("")&&tf[2].getText().equals("")){
			stu=dto.select();
			for (Departerment dep : stu) {
				dtm.addRow(new Object[]{String.valueOf(dep.getId()),dep.getName(),dep.getMaster()});
			}
			maxPageNumber=(int) Math.ceil(dtm.getRowCount()/pageSize);
			butPage[0].setEnabled(true);
			butPage[1].setEnabled(true);
			butPage[2].setEnabled(true);
			butPage[3].setEnabled(true);
			tf[0].setText("");
			tf[1].setText("");
			tf[2].setText("");
			infolab.setText("查询完成！");
			
			}else{
				Departerment dt=new Departerment();
				if(tf[0].getText().equals("")) dt.setId(null);
				else dt.setId(Integer.valueOf(tf[0].getText()));
				if(tf[1].getText().equals("")) dt.setName(null);
				else dt.setName(tf[1].getText());
				if(tf[2].getText().equals("")) dt.setMaster(null);
				else dt.setMaster(tf[2].getText());
				List<Departerment> stu1=dto.singleSelect(dt);
				for (int i=0;i<stu1.size();i++) {
					dtm.addRow(new Object[]{stu1.get(i).getId(),stu1.get(i).getName(),stu1.get(i).getMaster()});
				}
				crossBut[0].setEnabled(true);
				crossBut[1].setEnabled(true);
				infolab.setText("查询完成！");
			}
		}

		if (e.getSource() == but[1]) {
			while (dtm.getRowCount() != 0) {
				dtm.removeRow(0);
			}
			if(tf[0].getText().trim().equals("")||tf[1].getText().trim().equals("")||tf[2].getText().trim().equals("")) return;
			Departerment dt=new Departerment();
			dt.setId(Integer.valueOf(tf[0].getText()));
			dt.setName(tf[1].getText());
			dt.setMaster(tf[2].getText());
			dto.insert(dt);
			dtm.addRow(new Object[] { tf[0].getText(), tf[1].getText(), tf[2].getText() });
			infolab.setText("添加成功！");
			tf[0].setText("");
			tf[1].setText("");
			tf[2].setText("");
		}

		if (e.getActionCommand() == "修改") {
			while (dtm.getRowCount() != 0) {
				dtm.removeRow(0);
			}
			Departerment dt=new Departerment();
			dt.setName(tf[1].getText());
			dt.setMaster(tf[2].getText());
            dto.update(dt, Integer.valueOf(tf[0].getText()));
            DefaultTableModel newdtm=new DefaultTableModel();
			newdtm.setColumnIdentifiers(new Object[]{"ID","名称","负责人"});
			newdtm.addRow(new Object[]{tf[0].getText(),tf[1].getText(),tf[2].getText()});
			table.setModel(newdtm);
            infolab.setText("修改成功！");
            tf[0].setText("");
			tf[1].setText("");
			tf[2].setText("");
		}

		if (e.getActionCommand() == "删除") {
			while (dtm.getRowCount() != 0) {
				dtm.removeRow(0);
			}
			if(tf[0].getText().equals("")&&tf[2].getText().equals("")||tf[0].getText().equals("")&&tf[1].getText().equals("")||tf[0].getText().equals("")&&tf[1].getText()!=""&&tf[2].getText()!=""){
				int f=JOptionPane.showConfirmDialog(this, "你确定以此条件删除数据吗?","确认信息" , JOptionPane.YES_NO_OPTION);
				if(f==0){
					Departerment dt=new Departerment();
					if(tf[0].getText().equals("")) dt.setId(null);
					else dt.setId(Integer.valueOf(tf[0].getText()));
					if(tf[1].getText().equals("")) dt.setName(null);
					else dt.setName(tf[1].getText());
					if(tf[2].getText().equals("")) dt.setMaster(null);
					else dt.setMaster(tf[2].getText());
					dto.delete(dt);
					infolab.setText("删除成功！");
					tf[0].setText("");
					tf[1].setText("");
					tf[2].setText("");
				}else{
					tf[0].setText("");
					tf[1].setText("");
					tf[2].setText("");
					return;
				}
			}else{
				Departerment dt=new Departerment();
				if(tf[0].getText().equals("")) dt.setId(null);
				else dt.setId(Integer.valueOf(tf[0].getText()));
				if(tf[1].getText().equals("")) dt.setName(null);
				else dt.setName(tf[1].getText());
				if(tf[2].getText().equals("")) dt.setMaster(null);
				else dt.setMaster(tf[2].getText());
				dto.delete(dt);
				infolab.setText("删除成功！");
				tf[0].setText("");
				tf[1].setText("");
				tf[2].setText("");
			}
		}
		
		int currentCount=0;
		if(e.getSource()==crossBut[0]){
			crossBut[1].setEnabled(true);
			for(int i=0;i<stu.size();i++){
				if(stu.get(i).getId()==Integer.valueOf(tf[0].getText())){
					currentCount=i;
				}
			}
			currentCount--;
			if(currentCount<0) {crossBut[0].setEnabled(false);return;}
			DefaultTableModel newdtm=new DefaultTableModel();
			newdtm.setColumnIdentifiers(new Object[]{"ID","名称","负责人"});
			newdtm.addRow(new Object[]{String.valueOf(stu.get(currentCount).getId()),stu.get(currentCount).getName(),stu.get(currentCount).getMaster()});
		    table.setModel(newdtm);
		    
		    tf[0].setText(String.valueOf(stu.get(currentCount).getId()));
		    tf[1].setText(stu.get(currentCount).getName());
		    tf[2].setText(stu.get(currentCount).getMaster());
		}
		
		if(e.getSource()==crossBut[1]){
			crossBut[0].setEnabled(true);
			for(int i=0;i<stu.size();i++){
				if(stu.get(i).getId()==Integer.valueOf(tf[0].getText())){
					currentCount=i;
				}
			}
			currentCount++;
			System.out.println("size " + stu.size());
			System.out.println("currentCount " + currentCount );
			if(currentCount>stu.size()-1) {crossBut[1].setEnabled(false);return;}
			DefaultTableModel newdtm=new DefaultTableModel();
			newdtm.setColumnIdentifiers(new Object[]{"ID","名称","负责人"});
			newdtm.addRow(new Object[]{String.valueOf(stu.get(currentCount).getId()),stu.get(currentCount).getName(),stu.get(currentCount).getMaster()});
		    table.setModel(newdtm);
		    
		    tf[0].setText(String.valueOf(stu.get(currentCount).getId()));
		    tf[1].setText(stu.get(currentCount).getName());
		    tf[2].setText(stu.get(currentCount).getMaster());
		}
		
		if(e.getSource()==butPage[0]){
			currentPageNumber=1;
			Vector dataVector=dtm.getDataVector();
			DefaultTableModel newdtm=new DefaultTableModel();
			newdtm.setColumnIdentifiers(new Object[]{"ID","名称","负责人"});
			for(int i=0;i<pageSize;i++){
				newdtm.addRow((Vector) dataVector.elementAt(i));
			}
			table.setModel(newdtm);
			butPage[0].setEnabled(false);
			butPage[1].setEnabled(false);
			butPage[2].setEnabled(true);
			butPage[3].setEnabled(true);
		}
		
		if(e.getSource()==butPage[2]){
			currentPageNumber++;
			Vector dataVector=dtm.getDataVector();
			DefaultTableModel newdtm=new DefaultTableModel();
			newdtm.setColumnIdentifiers(new Object[]{"ID","名称","负责人"});
			if(maxPageNumber==currentPageNumber){
				int lastPageSize=(int) (dtm.getRowCount()-pageSize*(maxPageNumber-1));
				for(int i=0;i<lastPageSize;i++){
					newdtm.addRow((Vector) dataVector.elementAt((int) (pageSize*(maxPageNumber-1)+i)));
				}
			butPage[2].setEnabled(false);
			butPage[3].setEnabled(false);
			}else{
				for(int i=0;i<pageSize;i++){
				    newdtm.addRow((Vector) dataVector.elementAt((int) (pageSize*(currentPageNumber-1)+i)));
				}
			}
			table.setModel(newdtm);
			butPage[0].setEnabled(true);
			butPage[1].setEnabled(true);
		}
		
		if(e.getSource()==butPage[1]){
			currentPageNumber--;
			Vector dataVector=dtm.getDataVector();
			DefaultTableModel newdtm=new DefaultTableModel();
			newdtm.setColumnIdentifiers(new Object[]{"ID","名称","负责人"});
			for(int i=0;i<pageSize;i++){
				newdtm.addRow((Vector) dataVector.elementAt((int) (pageSize*(currentPageNumber-1)+i)));
			}
			table.setModel(newdtm);
			if(currentPageNumber==1){
				butPage[0].setEnabled(false);
				butPage[1].setEnabled(false);
			}
			butPage[2].setEnabled(true);
			butPage[3].setEnabled(true);
		}
		
		if(e.getSource()==butPage[3]){
			currentPageNumber=maxPageNumber;
			Vector dataVector=dtm.getDataVector();
			DefaultTableModel newdtm=new DefaultTableModel();
			newdtm.setColumnIdentifiers(new Object[]{"ID","名称","负责人"});
			int lastPageNumber=(int) (dtm.getRowCount()-pageSize*(maxPageNumber-1));
			for(int i=0;i<lastPageNumber;i++){
				newdtm.addRow((Vector) dataVector.elementAt((int) (pageSize*(maxPageNumber-1)+i)));
			}
			table.setModel(newdtm);
			butPage[0].setEnabled(true);
			butPage[1].setEnabled(true);
			butPage[2].setEnabled(false);
			butPage[3].setEnabled(false);
		}
	}
}
